package com.thinkfit.jy.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;

import com.common.lib.bean.ApplicationBean;

import java.util.ArrayList;
import java.util.List;

public class RunningAppUtils {

    static RunningAppUtils runningAppUtils;
    ActivityManager manager;
    Context mContext;
    ArrayList<ApplicationBean> applicationBeanList;
    ArrayList<ApplicationBean> homeAppList;
    ArrayList<ApplicationBean> currentRunningApp = new ArrayList<>();
    private static int OPEN_APP_NUM = 1;//开启应用数
    List<ActivityManager.RunningAppProcessInfo> runningAppProcesses;

    public static RunningAppUtils getInstance(){
        if(runningAppUtils == null){
            runningAppUtils = new RunningAppUtils();
        }
        return runningAppUtils;
    }

    public void init(Context context){
        mContext = context;
        manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        currentRunningApp.clear();
//        for (ActivityManager.RunningAppProcessInfo processInfo : runningAppProcesses){
//            int pid = processInfo.pid;
//            int uid = processInfo.uid;
//            String name = processInfo.processName;
//            int level = processInfo.lastTrimLevel;
//        }
        getAppList();
    }

    /**
     * 获取正在进行进程中有哪些后台配置的应用
     */
    public void getAppList(){
        runningAppProcesses = manager.getRunningAppProcesses();
        PackageManager pm = mContext.getPackageManager();
//        List<PackageInfo> packages = pm.getInstalledPackages(0);
        if(applicationBeanList != null && applicationBeanList.size() > 0){
            for (ApplicationBean applicationBean : applicationBeanList){
                for (int i = 0;i<runningAppProcesses.size();i++){
//                    System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa  packages : "+runningAppProcesses.get(i).processName);
                    if(applicationBean.getAppid().equals(runningAppProcesses.get(i).processName)){
                        if(currentRunningApp.size() < OPEN_APP_NUM){
                            currentRunningApp.add(applicationBean);
                        }
                    }
                }
            }
        }

//        Observable.create(new ObservableOnSubscribe<List<ApplicationBean>>() {
//            @SuppressLint("CheckResult")
//            @Override
//            public void subscribe(ObservableEmitter<List<ApplicationBean>> emitter) throws Exception {
//                runningAppProcesses = manager.getRunningAppProcesses();
//                PackageManager pm = mContext.getPackageManager();
//                List<PackageInfo> packages = pm.getInstalledPackages(0);
//                if(applicationBeanList != null && applicationBeanList.size() > 0){
//                    for (ApplicationBean applicationBean : applicationBeanList){
//                        for (int i = 0;i<runningAppProcesses.size();i++){
////                    System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa  packages : "+runningAppProcesses.get(i).processName);
//                            if(applicationBean.getAppid().equals(runningAppProcesses.get(i).processName)){
//                                currentRunningApp.add(applicationBean);
//                            }
//                        }
//                    }
//                }
//                emitter.onNext(currentRunningApp);
//            }
//        }).subscribe(new Observer<List<ApplicationBean>>() {
//            @Override
//            public void onSubscribe(Disposable d) {
//
//            }
//
//            @Override
//            public void onNext(List<ApplicationBean> applicationBeans) {
//
//            }
//
//            @Override
//            public void onError(Throwable e) {
//
//            }
//
//            @Override
//            public void onComplete() {
//
//            }
//        });


    }

    /**
     * 获取当前进行的应用
     * @return
     */
    public ArrayList<ApplicationBean> getCurrentRunningApp(){
        return currentRunningApp;
    }

    public boolean isOpen(String packageName){
        RunningAppUtils.getInstance().init(mContext);
        boolean isOpen = false;
        if(currentRunningApp.size() <= 0){
            return isOpen;
        }
        for (ApplicationBean applicationBean : currentRunningApp){
            if(applicationBean.getAppid().equals(packageName)){
                isOpen = true;
               return isOpen;
            }
        }
        return isOpen;
    }

    /**
     * 后台应用列表
     * @param list
     */
    public void setApplicationBeanList(ArrayList<ApplicationBean> list){
        applicationBeanList = list;
    }
    /**
     * 后台应用列表
     */
    public ArrayList<ApplicationBean> getApplicationBeanList(){
        return applicationBeanList;
    }

    /**
     * 首页显示应用列表
     * @param list
     */
    public  void setHomeApplicationBeanList(ArrayList<ApplicationBean> list){
        homeAppList = list;
    }

    public ArrayList<ApplicationBean> getHomeApplicationBeanList(){
        return homeAppList;
    }

}
